package devtool

import (
	"bufio"
	"fmt"
	"regexp"
	"strings"
)

//AddJsonFormGormTagName  为model添加常用tag
func AddJsonFormGormTagName(in string, tags []string) string {
	var result string
	scanner := bufio.NewScanner(strings.NewReader(in))
	var oldLineTmp = ""
	var lineTmp = ""
	var propertyTmp = ""
	var seperateArr []string
	for scanner.Scan() {
		oldLineTmp = scanner.Text()

		lineTmp = strings.Trim(scanner.Text(), " ")
		if strings.Contains(lineTmp, "{") || strings.Contains(lineTmp, "}") {
			result = result + oldLineTmp + "\n"
			continue
		}
		seperateArr = Split(lineTmp, " ")
		seperateArrTemp := []string{}
		for _, v := range seperateArr {
			seperateArrTemp = append(seperateArrTemp, Split(v, "\t")...)
		}
		seperateArr = seperateArrTemp
		// 接口或者父类声明不参与tag, 自带tag不参与tag
		if len(seperateArr) <= 1 {
			result = result + oldLineTmp + "\n"
			continue
		} else {
			tagstr := ""
			if len(seperateArr) >= 3 {
				seperatetmp := seperateArr[2]
				if strings.HasPrefix(seperatetmp, "`") && strings.HasSuffix(seperatetmp, "`") {
					tagstr = strings.Join(seperateArr[2:], " ")
					tagstr = strings.Replace(tagstr, "`", "", -1)
				}
			}
			propertyTmp = HumpToUnderLine(seperateArr[0])

			for _, tag := range tags {
				reg := regexp.MustCompile("( |`)" + tag + ":")

				contain := reg.FindString(tagstr)
				// if !strings.Contains(tagstr, tag+":") {
				if len(contain) == 0 {
					tagstr = tagstr + fmt.Sprintf(" %s:\"%s\"", tag, propertyTmp)
				}
			}
			tagstr = fmt.Sprintf("    `%s`", tagstr)
			// seperateArr2 := append(seperateArr[:2], tagstr)
			// if len(seperateArr) >= 3 {
			// 	seperateArr2 = append(seperateArr, seperateArr[3:]...)
			// }
			// fmt.Println("lll=", seperateArr[:2], "rrrr=", seperateArr[3:], len(seperateArr))
			// for i, v := range seperateArr {
			// 	fmt.Println(i, v)
			// }
			// result = result + "\t" + strings.Join(seperateArr2, "\t\t") + "\n"
			result = result + "\t" + seperateArr[0] + "\t" + seperateArr[1] + "\t" + tagstr + "\n"

		}
	}
	return result
}

//为model添加常用tag
func AddJsonFormGormTag(in string) string {
	var result string
	scanner := bufio.NewScanner(strings.NewReader(in))
	var oldLineTmp = ""
	var lineTmp = ""
	var propertyTmp = ""
	var seperateArr []string
	for scanner.Scan() {
		oldLineTmp = scanner.Text()

		lineTmp = strings.Trim(scanner.Text(), " ")
		if strings.Contains(lineTmp, "{") || strings.Contains(lineTmp, "}") {
			result = result + oldLineTmp + "\n"
			continue
		}
		seperateArr = Split(lineTmp, " ")
		seperateArrTemp := []string{}
		for _, v := range seperateArr {
			seperateArrTemp = append(seperateArrTemp, Split(v, "\t")...)
		}
		seperateArr = seperateArrTemp

		// 接口或者父类声明不参与tag, 自带tag不参与tag
		if len(seperateArr) == 1 || len(seperateArr) == 3 {
			continue
		}
		propertyTmp = HumpToUnderLine(seperateArr[0])
		oldLineTmp = oldLineTmp + fmt.Sprintf("    `gorm:\"column:%s\" json:\"%s\" form:\"%s\"`", propertyTmp, propertyTmp, propertyTmp)
		result = result + oldLineTmp + "\n"
	}
	return result
}

// 增强型split，对  a,,,,,,,b,,c     以","进行切割成[a,b,c]
func Split(s string, sub string) []string {
	var rs = make([]string, 0, 20)
	tmp := ""
	Split2(s, sub, &tmp, &rs)
	return rs
}

// 附属于Split，可独立使用
func Split2(s string, sub string, tmp *string, rs *[]string) {
	s = strings.Trim(s, sub)
	if !strings.Contains(s, sub) {
		*tmp = s
		*rs = append(*rs, *tmp)
		return
	}
	for i, _ := range s {
		if string(s[i]) == sub {
			*tmp = s[:i]
			*rs = append(*rs, *tmp)
			s = s[i+1:]
			Split2(s, sub, tmp, rs)
			return
		}
	}
}

// camel string, xx_yy to XxYy
func camelString(s string) string {
	data := make([]byte, 0, len(s))
	j := false
	k := false
	num := len(s) - 1
	for i := 0; i <= num; i++ {
		d := s[i]
		if k == false && d >= 'A' && d <= 'Z' {
			k = true
		}
		if d >= 'a' && d <= 'z' && (j || k == false) {
			d = d - 32
			j = false
			k = true
		}
		if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
			j = true
			continue
		}
		data = append(data, d)
	}
	return string(data[:])
}

// 驼峰转下划线
func HumpToUnderLine(s string) string {
	if s == "ID" {
		return "id"
	}
	var rs string
	elements := FindUpperElement(s)
	for _, e := range elements {
		s = strings.Replace(s, e, "_"+strings.ToLower(e), -1)
	}
	rs = strings.Trim(s, " ")
	rs = strings.Trim(rs, "\t")
	return strings.Trim(rs, "_")
}

// 找到字符串中大写字母的列表,附属于HumpToUnderLine
func FindUpperElement(s string) []string {
	var rs = make([]string, 0, 10)
	for i := range s {
		if s[i] >= 65 && s[i] <= 90 {
			rs = append(rs, string(s[i]))
		}
	}
	return rs
}
